package com.sugon.quartz.misfire;

import org.apache.commons.lang3.time.DateUtils;

import java.util.Date;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import com.sugon.quartz.HelloJob;

/**
 * @desc:
 * @author: luoy
 * @date: 2021-12-02 16:15
 */
public class MisfireTest {

    public static void main(String[] args) throws SchedulerException {

        //创建一个scheduler
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        //创建一个Trigger
        Date next = DateUtils.addSeconds(new Date(), -100);
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger", "g1")
                .startAt(next)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()

                   //    .withMisfireHandlingInstructionFireNow()//立即执行job，即在发现了misfire以后立即执行job。
                    //   .withMisfireHandlingInstructionNextWithExistingCount() //不会执行job。此命令会等待下一次执行时间来执行job

                )
                .build();
        //创建一个job
        JobDetail job = JobBuilder.newJob(MisfireJob.class)
                //jobDataMap实为一个map
                .withIdentity("myjob", "mygroup")
                .build();

        //注册trigger并启动scheduler
        scheduler.scheduleJob(job, trigger);
        scheduler.start();

    }
}